let requsetUrls = []

let count = 0

export function requestData(url, data, method = 'POST', access_token, user_id) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: url.indexOf('https') === -1 ? 'https://ya30api.hbwlife.com/wxmp' + url : url,
			method: method,
			header: {
				"AUTH-TOKEN": user_id + "|" + access_token
			},
			data: data,
			success: (res) => {
				if (typeof res?.data == 'object' && !Array.isArray(res?.data) && res?.data?.code ==
					0 || Array.isArray(res?.data)) {
					resolve(res)
				} else if (res?.data?.code == -57004) {
					//reject("无权操作")
					// uni.showToast({
					// 	title: '请先绑定手机号',
					// 	icon: 'none',
					// });
					// setTimeout(() => {
					// 	uni.navigateTo({
					// 		url: '/pages/loginPhone/loginPhone',
					// 	})
					// }, 1000)
				} else if (res?.data?.code == -56055) {
					reject("Token失效")
				} else {
					reject(res?.data || {})
				}
			},
			fail: () => {}
		})
	})
}

function refershRequest(isLogin = false, data) {
	return new Promise((resolve, reject) => {
		if (requsetUrls.length) {
			let {
				url,
				data,
				method,
			} = requsetUrls[0]
			if (!isLogin) {
				uni.login({
					success: (res) => {
						requsetUrls.shift()
						//console.log(res.code)
						uni.request({
							url: 'https://ya30api.hbwlife.com/wxmp/wxaccess/mp/user/login',
							method: 'POST',
							header: {},
							data: {
								"wxlogin_code": res.code
							},
							success: (res) => {
								uni.removeStorageSync('user_id');
								uni.removeStorageSync('access_token');
								uni.removeStorageSync('user_id');
								uni.setStorageSync('user_id', res.data.datas
									.user_id);
								uni.setStorageSync('user_id', res.data.datas
									.user_id);
								uni.setStorageSync('access_token', res.data
									.datas.access_token);
								uni.setStorageSync('mobile_binded', res.data
									.datas.mobile_binded);
								requestData(
									url,
									data,
									method,
									res.data.datas.access_token,
									res.data.datas.user_id
								).then(res => {
									resolve(res)
								}).catch(err => {
									reject(err)
								})
							},
							fail: () => {

							}
						})
					},
					fail: (err) => {
						console.log(err)
					}
				})
			} else {
				let user_id = uni.getStorageSync('user_id');
				let access_token = uni.getStorageSync('access_token');
				requestData(
					url,
					data,
					'POST',
					access_token,
					user_id
				).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			}
		} else {
			resolve(data)
		}
	})
}

export function request(url, data, method = 'POST') {
	return new Promise((resolve, reject) => {
		let user_id = uni.getStorageSync('user_id');
		let access_token = uni.getStorageSync('access_token');
		uni.request({
			url: 'https://ya30api.hbwlife.com/wxmp' + url,
			method: method,
			header: {
				"AUTH-TOKEN": user_id + "|" + access_token
			},
			data: data,
			success: (res) => {
				if(url.indexOf('/mp/user/getPhoneNumber') !== -1) {
					res.data.code = -57110
					resolve(res.data)
				}
				else {
					//console.log(res.data.code)
					if (res?.data?.code == 0) {
						refershRequest(true, res?.data).then(res => {
							resolve(res)
						}).catch(err => {
							reject(err)
						})
					}
					else if (res?.data?.code == -57004) {
						//reject("无权操作")
						// uni.showToast({
						// 	title: '请先绑定手机号',
						// 	icon: 'none',
						// });
						// setTimeout(() => {
						// 	uni.navigateTo({
						// 		url: '/pages/loginPhone/loginPhone',
						// 	})
						// }, 1000)
					}
					else if (res?.data?.code == -56055) {
						// reject("Token失效")
						let index = requsetUrls.findIndex(item => item.url == url)
						if (index == -1) {
							requsetUrls.push({
								url,
								data,
								method
							})
						}
						refershRequest().then(res => {
							resolve(res)
						}).catch(err => {
							reject(err)
						})
					}
					else if (res?.data?.code == -57110) {
						// reject("Token失效")
						let index = requsetUrls.findIndex(item => item.url == url)
						if (index == -1) {
							requsetUrls.push({
								url,
								data,
								method
							})
						}
						refershRequest().then(res => {
							resolve(res)
						}).catch(err => {
							reject(err)
						})
					}
					else {
						let index = requsetUrls.findIndex(item => item.url == url)
						if (index == -1) {
							requsetUrls.push({
								url,
								data,
								method
							})
						}
						refershRequest().then(res => {
							resolve(res)
						}).catch(err => {
							reject(err?.msg || '网络错误')
						})
					}
				}


			},
			complete: () => {},
			fail: (err) => {
				if(url.indexOf('/mp/user/getPhoneNumber') !== -1) {
					reject(err)
				}
				else {
					let index = requsetUrls.findIndex(item => item.url == url)
					if (index == -1) {
						requsetUrls.push({
							url,
							data,
							method
						})
					}
					refershRequest().then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				}

			}
		})
	})
}